﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class ReportDetail : Form
    {
        private DataTable table;
        private PurchaseDao dao;
        private int rowNumber = 0;
        public ReportDetail()
        {
            InitializeComponent();
            dao = new PurchaseDao();
            rdbNew.Checked = true;
            dtFrom.Enabled = true;
            dtTo.Enabled = true;
            nbMonth.Enabled = false;
        }

        
        private void txtTotal_TextChanged(object sender, EventArgs e)
        {

        }

        private DataGridViewImageCell loadImage(string url)
        {
            try
            {
                Image image = Image.FromFile(url);
                DataGridViewImageCell imageCell = new DataGridViewImageCell();
                Image imagedis = resizeImage(image, new Size(150, 150));
                imageCell.Value = imagedis;
                image.Dispose();
                return imageCell;
            }
            catch
            {
                return new DataGridViewImageCell();
            }
        }

        private Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }
        private void init()
        {
            if (rowNumber == 0)
            {
                dtgCustomers.Rows.Clear();
            }
            if (rdbNew.Checked)
            {
                table = dao.getCustommerDuringTime(dtFrom.Value, dtTo.Value);
            }
            else
            {
                table = dao.getCustommerAw((int)nbMonth.Value);
            }

            if (table != null && table.Rows.Count > 0)
            {
                txtTotal.Text = table.Rows.Count.ToString();
                DataRow row = null;
                for (int j = rowNumber * 10; j < (rowNumber + 1) * 10; j++)
                {
                    if (j >= table.Rows.Count)
                    {
                        break;
                    }
                    row = table.Rows[j];
                    if (row["NAME"].ToString().ToLower().Contains(txtSearch.Text.Trim()))
                    {
                        int i = dtgCustomers.Rows.Add();

                        dtgCustomers.Rows[i].Cells[0].Value = row["ID"].ToString();
                        dtgCustomers.Rows[i].Cells[1].Value = i + 1;
                        dtgCustomers.Rows[i].Cells[2] = loadImage(row["PICTURE"].ToString());
                        dtgCustomers.Rows[i].Cells[3].Value = row["NAME"].ToString();
                        dtgCustomers.Rows[i].Cells[4].Value = row["NOTE"].ToString();
                        dtgCustomers.Rows[i].Cells[5].Value = DateTime.Parse(row["CREATE_DATE"].ToString()).ToString("dd/MM/yyyy");
                        if (row["DAY_ONLY"] != null && row["DAY_ONLY"].ToString() != "" && row["DAY_ONLY"].ToString().Equals("T"))
                        {
                            dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.Violet;
                        }
                        if (j == table.Rows.Count - 1)
                        {
                            break;
                        }
                        if (i % 2 == 1)
                        {
                            dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                        }
                    }
                }
            }
            else
            {
                txtTotal.Text = "0";
            }
        }
        private void btnView_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            rowNumber = 0;
            init();
        }

        private void cbxInMonth_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbNew.Checked)
            {
                nbMonth.Enabled = false;
                dtFrom.Enabled = true;
                dtTo.Enabled = true;
            }
            else
            {
                nbMonth.Enabled = true;
                dtFrom.Enabled = false;
                dtTo.Enabled = false;
            }
            if (rdbAllways.Checked)
            {
                nbMonth.Enabled = true;
                dtFrom.Enabled = false;
                dtTo.Enabled = false;
            }
            else
            {
                nbMonth.Enabled = false;
                dtFrom.Enabled = true;
                dtTo.Enabled = true;
            }
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            dtgCustomers.Rows.Clear();
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    
                        int i = dtgCustomers.Rows.Add();
                        dtgCustomers.Rows[i].Cells[0].Value = row["ID"].ToString();
                        dtgCustomers.Rows[i].Cells[1].Value = i + 1;
                        dtgCustomers.Rows[i].Cells[2] = loadImage(row["PICTURE"].ToString());
                        dtgCustomers.Rows[i].Cells[3].Value = row["NAME"].ToString();
                        dtgCustomers.Rows[i].Cells[4].Value = row["NOTE"].ToString();
                        dtgCustomers.Rows[i].Cells[5].Value = DateTime.Parse(row["CREATE_DATE"].ToString()).ToShortDateString();
                        if (row["DAY_ONLY"] != null && row["DAY_ONLY"].ToString() != "" && row["DAY_ONLY"].ToString().Equals("T"))
                        {
                            dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.Violet;
                        }
                    
                }
            }
        }

        private void dtgCustomers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 2)
            {
                int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[0].Value.ToString());
                Purchase p = new Purchase(id);
                p.ShowDialog();
                rowNumber = 0;
                init();
            }
            else if (e.RowIndex >= 0 && e.ColumnIndex == 3)
            {
                int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[0].Value.ToString());
                UpdateCustomer p = new UpdateCustomer(id);
                p.ShowDialog();
                rowNumber = 0;
                init();
            }
            else if (e.RowIndex >= 0 && e.ColumnIndex == 4)
            {
                int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[0].Value.ToString());
                CustomerDetail p = new CustomerDetail(id);
                p.ShowDialog();
                rowNumber = 0;
                init();
            }
        }

        private void ReportDetail_Load(object sender, EventArgs e)
        {

        }

        private void dtgCustomers_Scroll(object sender, ScrollEventArgs e)
        {
            if (dtgCustomers.FirstDisplayedScrollingRowIndex + 5 >= (rowNumber + 1) * 10)
            {
                rowNumber++;
                init();
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            
        }

        private void btnFilter_Click_1(object sender, EventArgs e)
        {
            rowNumber = 0;
            dtgCustomers.Rows.Clear();
            if (table != null && table.Rows.Count > 0)
            {
                txtTotal.Text = table.Rows.Count.ToString();
                DataRow row = null;
                for (int j = rowNumber * 10; table.Rows.Count < 10 ? j< table.Rows.Count : j < (rowNumber + 1) * 10; j++)
                {
                    row = table.Rows[j];
                    if (j >= table.Rows.Count)
                    {
                        break;
                    }
                    if (row["NAME"].ToString().ToLower().Contains(txtSearch.Text.ToLower().Trim()))
                    {
                       
                        int i = dtgCustomers.Rows.Add();                        
                        dtgCustomers.Rows[i].Cells[0].Value = row["ID"].ToString();
                        dtgCustomers.Rows[i].Cells[1].Value = i + 1;
                        dtgCustomers.Rows[i].Cells[2] = loadImage(row["PICTURE"].ToString());
                        dtgCustomers.Rows[i].Cells[3].Value = row["NAME"].ToString();
                        dtgCustomers.Rows[i].Cells[4].Value = row["NOTE"].ToString();
                        dtgCustomers.Rows[i].Cells[5].Value = DateTime.Parse(row["CREATE_DATE"].ToString()).ToShortDateString();
                        if (row["DAY_ONLY"] != null && row["DAY_ONLY"].ToString() != "" && row["DAY_ONLY"].ToString().Equals("T"))
                        {
                            dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.Violet;
                        }
                        if (j == table.Rows.Count - 1)
                        {
                            break;
                        }
                        if (i % 2 == 1)
                        {
                            dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                        }
                    }
                }
            }
            else
            {
                txtTotal.Text = "0";
            }
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void rdbNew_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbNew.Checked)
            {
                nbMonth.Enabled = false;
                dtFrom.Enabled = true;
                dtTo.Enabled = true;
            }
            else
            {
                nbMonth.Enabled = true;
                dtFrom.Enabled = false;
                dtTo.Enabled = false;
            }
            if (rdbAllways.Checked)
            {
                nbMonth.Enabled = true;
                dtFrom.Enabled = false;
                dtTo.Enabled = false;
            }
            else
            {
                nbMonth.Enabled = false;
                dtFrom.Enabled = true;
                dtTo.Enabled = true;
            }
        }

        private void rdbAllways_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbNew.Checked)
            {
                nbMonth.Enabled = false;
                dtFrom.Enabled = true;
                dtTo.Enabled = true;
            }
            else
            {
                nbMonth.Enabled = true;
                dtFrom.Enabled = false;
                dtTo.Enabled = false;
            }
            if (rdbAllways.Checked)
            {
                nbMonth.Enabled = true;
                dtFrom.Enabled = false;
                dtTo.Enabled = false;
            }
            else
            {
                nbMonth.Enabled = false;
                dtFrom.Enabled = true;
                dtTo.Enabled = true;
            }
        }
    }
}
